(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("settleInitCustpayService", settleInitCustpayService);
	settleInitCustpayService.$inject = [ "$filter", "clsConstants", "payDetailService", "txnService" ];
	function settleInitCustpayService($filter, clsConstants, payDetailService, txnService) {

		function getCompanyCodeByAlloc(settlement, allocCode) {
			var custCode = "", encode = $filter("encode");
			if (allocCode == "CUSTOMER" || allocCode == "PAYEE")
				custCode = encode(settlement.custpay.payeeCode);
			else if (allocCode == "OBLIGOR" || allocCode == "PAYER")
				custCode = encode(settlement.custpay.payerCode);
			else
				custCode = encode(settlement.custpay.payerCode);
			return custCode;
		}

		function getCcy(settlement) {
			return settlement.custpay.payCcy;
		}
		function getTxnAmount(settlement) {
			return settlement.custpay.payAmt;
		}
		function getAssetId(settlement) {
			return settlement.custpay.assetId;
		}
		function getDeductIntAndFeeFlag(settlement) {
			return {
				deductIntFlag : "Y",
				deductFeeFlag : "Y"
			};
		}
		function getProdCodeAndType(settlement) {
			var prodType = settlement.custpay.prodType;
			return {
				prodCode : "",
				prodType : prodType
			};
		}
		function initSettlement(settlement) {
			var paydetails = settlement.payDetails;
			var custpay = settlement.custpay;
			var couplePrimaryFlag = false;
			var coupleSlaveFlag = false;
			angular.forEach(paydetails, function(paydetail, index, array) {
				if (clsConstants.PayDetailItemSubType.COUPLE_PRIMARY == paydetail.itemSubtype) {
					couplePrimaryFlag = true;
				}
				if (clsConstants.PayDetailItemSubType.COUPLE_SLAVE == paydetail.itemSubtype) {
					coupleSlaveFlag = true;
				}
			});

			if (couplePrimaryFlag == false) {
				var payDetail = {};
				payDetail.crDrFlag = "D";
				payDetail.payCcy = custpay.payCcy;
				payDetail.payAmt = custpay.payAmt;
				payDetail.allocCode = "PAYER";
				var length = settlement.payDetails.length;
				payDetail.payItemSeq = length == 0 ? 1 : settlement.payDetails[length - 1].payItemSeq + 1;
				payDetail.itemType = "P";
				payDetail.itemSubtype = clsConstants.PayDetailItemSubType.COUPLE_PRIMARY;
				// set values to new item"s hidden fields: siteCode,setlId
				payDetail.siteCode = settlement.settlementInfo.siteCode;
				payDetail.setlId = settlement.settlementInfo.setlId;
				settlement.payDetails.push(payDetail);

				payDetailService.setPayDetailAcctInfo(settlement, payDetail, null, false);
				payDetail.splitPayDetail_Show = payDetailService.isShowSplitPayDetail(settlement, payDetail);
			}
			if (coupleSlaveFlag == false) {
				var payDetail = {};
				payDetail.crDrFlag = "C";
				payDetail.payCcy = custpay.payCcy;
				payDetail.payAmt = custpay.payAmt;
				payDetail.allocCode = "PAYEE";
				var length = settlement.payDetails.length;
				payDetail.payItemSeq = length == 0 ? 1 : settlement.payDetails[length - 1].payItemSeq + 1;
				payDetail.itemType = "P";
				payDetail.itemSubtype = clsConstants.PayDetailItemSubType.COUPLE_SLAVE;
				// set values to new item"s hidden fields: siteCode,setlId
				payDetail.siteCode = settlement.settlementInfo.siteCode;
				payDetail.setlId = settlement.settlementInfo.setlId;

				settlement.payDetails.push(payDetail);

				payDetailService.setPayDetailAcctInfo(settlement, payDetail, null, false);
				payDetail.splitPayDetail_Show = payDetailService.isShowSplitPayDetail(settlement, payDetail);
			}

		}

		function getGeneralConfig(layoutConfig) {
			var generalConfig = angular.copy(layoutConfig.formConfigs.custPayInfo);
			return generalConfig;
		}
		
		function getBackMessage(){
			return "title.infobar.custpay.update";
		}
		
		function getLayout(settlement, packageSuffix) {
			return txnService.getLayout("settlement", packageSuffix, false, "CUSTPAY", settlement.custpay.prodType, undefined);
		}
		
		function getGeneralModel(settlement) {
			return settlement.custpay;
		}
		
		function processDynaparmConfig(loyout){
			angular.forEach(loyout.formConfigs.custPayInfo.gridConfigs, function(gridConfig){
				if(gridConfig.model.indexOf("dynamicParams.") != -1){
					gridConfig.model = "custpay." + gridConfig.model;
				}
			});
		}

		return {
			getCcy : getCcy,
			getDeductIntAndFeeFlag : getDeductIntAndFeeFlag,
			getTxnAmount : getTxnAmount,
			getAssetId : getAssetId,
			getCompanyCodeByAlloc : getCompanyCodeByAlloc,
			initSettlement : initSettlement,
			getProdCodeAndType : getProdCodeAndType,
			getGeneralConfig : getGeneralConfig,
			getBackMessage : getBackMessage,
			getLayout : getLayout,
			getGeneralModel : getGeneralModel,
			processDynaparmConfig : processDynaparmConfig
		};
	}
})(angular);
